package com.example.hospitalManage.controller.user.api;

import com.example.hospitalManage.config.annotation.TokenToNormalUser;
//import com.example.hospitalManage.controller.admin.OrderOrderInfoController;
import com.example.hospitalManage.entity.Department;
import com.example.hospitalManage.entity.OrderOrderInfo;
import com.example.hospitalManage.entity.Schedule;
import com.example.hospitalManage.entity.UserInfo;
import com.example.hospitalManage.service.UserAppointService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import com.example.hospitalManage.utils.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @projectName: hospitalManage
 * @package: com.example.hospitalManage.controller.user.api
 * @className: UserAppointController
 * @author: GCT
 * @description: TODO
 * 用户预约挂号功能接口
 * @date: 2022/12/31 12:24
 * @version: 1.0
 */
@RestController //=@ResponseBody+@Controller
@RequestMapping(value = "/api")
@Api(tags = "预约挂号功能相关接口")
public class UserAppointController {

    @Autowired
    UserAppointService userAppointService;



    //定义日志对象
    public final static Logger log =
            LoggerFactory.getLogger(UserAppointController.class);


    //GET请求：根据传入的医院名称返回该医院中的科室信息
//    @GetMapping(value = "/Department/{hosname}")
    @RequiresPermissions("user:list")
    @GetMapping(value = "/Department")
    @ApiOperation("根据传入的医院名称返回该医院中的科室信息")
//    public ResponseResult<List<Department>> getDepartmentByHosname(@PathVariable("hosname") String hosname){
    public ResponseResult<List<Department>> getDepartmentByHosname(String hosname){
        List<Department> departmentByHosname = userAppointService.getDepartmentByHosname(hosname);
        System.out.println(departmentByHosname);
        log.info("===================");
        return Response.createOkResp(departmentByHosname);
    }


    //GET请求：根据传入的科室名称返回该科室中的医生排班等信息
//    @GetMapping(value = "/Schedule/{depname}")
    @RequiresPermissions("user:list")
    @GetMapping(value = "/Schedule")
    @ApiOperation("根据传入的科室名称返回该科室中的医生排班等信息")
    public ResponseResult<List<Schedule>> getScheduleByDepname(String depname){
        List<Schedule> scheduleByDepname = userAppointService.getScheduleByDepname(depname);
        System.out.println(scheduleByDepname);
        log.info("===================");
        return Response.createOkResp(scheduleByDepname);
    }

    //POST请求，处理页面表单中提交的数据，向数据库增加数据
//    @RequiresPermissions("user:create")
    @PostMapping(value = "/userAppoint")
    @ApiOperation("新建订单，传入医院，科室，医生，时间等信息，完成预约挂号操作")
    public ResponseResult<OrderOrderInfo> addOrder(@TokenToNormalUser UserInfo userInfo,@Valid OrderOrderInfo orderOrderInfo){
        UUIDUtil uuidUtil = new UUIDUtil();
        long id = userInfo.getId();
        orderOrderInfo.setUserId(id);
        orderOrderInfo.setOutTradeNo(uuidUtil.getUUID());
        orderOrderInfo.setHoscode(userAppointService.getHoscodeByHosname(orderOrderInfo.getHosname()));
        orderOrderInfo.setDepcode(userAppointService.getDepcodeByDepname(orderOrderInfo.getDepname()));
        orderOrderInfo.setHosScheduleId(uuidUtil.getUUID());
//        System.out.println(orderOrderInfo.getPatientName());
        orderOrderInfo.setPatientId(userAppointService.getPatientIdByPatientName(orderOrderInfo.getPatientName()));
        orderOrderInfo.setHosRecordId(uuidUtil.getUUID());
        orderOrderInfo.setOrderStatus(0);

        boolean save = userAppointService.save(orderOrderInfo);
        System.out.println(save);
        return Response.createOkResp("添加成功",orderOrderInfo);
    }



}
